package com.siao.component
{
	import com.siao.data.HashMap;
	import com.siao.errors.SingletonError;
	import com.siao.events.DialogEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	[Event(name="added", type="com.siao.events.DialogEvent")]
	
	[Event(name="removed", type="com.siao.events.DialogEvent")]
	
	/**
	 * 对话框管理器
	 * @author SiaoLeon
	 */
	public class DialogManager extends EventDispatcher
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		private static var _instance:DialogManager;
		
		/**
		 * 获取单例对象
		 */
		public static function get instance():DialogManager
		{
			if (_instance == null)
			{
				_instance = new DialogManager();
			}
			return _instance;
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var dialogs:HashMap;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数，单例模式
		 */
		public function DialogManager()
		{
			if (_instance != null)
			{
				throw new SingletonError(this);
			}
			_instance = this;
			
			this.init();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 获取指定对话框容器的队列
		 * @param container 对话框容器
		 * @return
		 */
		public function getQueue(container:DisplayObjectContainer):Vector.<Dialog>
		{
			return this.dialogs.getValue(container);
		}
		
		/**
		 * 添加对话框到指定容器
		 * @param container 对话框容器
		 * @param dialog 对话框对象
		 * @param index 指定添加索引
		 */
		public function add(container:DisplayObjectContainer, dialog:Dialog, index:int=-1):void
		{
			var queue:Vector.<Dialog> = this.dialogs.getValue(container);
			
			if (queue == null)
			{
				queue = new Vector.<Dialog>();
				this.dialogs.put(container, queue);
			}
			
			if (index == 0)
			{
				var current:Dialog = queue.length > 0 ? queue[0] : null;
				
				if (current != null && container.contains(current))
				{
					current.removeEventListener(Event.REMOVED_FROM_STAGE, onDialogRemoved);
					current.remove();
				}
				
				queue.unshift(dialog);
				
			}
			else if (index < 0 || index >= queue.length)
			{
				queue.push(dialog);
			}
			else
			{
				queue.splice(index, 0, dialog);
			}
			
			this.progress(container);
		}
		
		/**
		 * 移除指定容器的对话框
		 * @param container 对话框容器
		 * @param dialog 对话框对象
		 */
		public function remove(container:DisplayObjectContainer, dialog:Dialog):void
		{
			var queue:Vector.<Dialog> = this.dialogs.getValue(container);
			
			if (queue == null)
			{
				return;
			}
			
			if (queue.length <= 0)
			{
				this.dialogs.remove(container);
				return;
			}
			
			for (var i:int = queue.length - 1; i >= 0; i--)
			{
				if (queue[i] == dialog)
				{
					queue.splice(i, 1);
				}
			}
			
			if (container.contains(dialog))
			{
				dialog.remove();
			}
		}
		
		/**
		 * 清除指定容器的对话框队列，如果容器为空则清除所有对话框
		 * @param container 对话框容器
		 */
		public function clear(container:DisplayObjectContainer=null):void
		{
			if (container == null)
			{
				var containers:Array = this.dialogs.keys;
				
				while (containers.length > 0)
				{
					this.clear(containers.shift());
				}
				
				return;
			}
			
			var queue:Vector.<Dialog> = this.dialogs.getValue(container);
			
			if (queue == null)
			{
				return;
			}
			
			this.dialogs.remove(container);
			
			if (queue.length > 0)
			{
				var current:Dialog = queue[0];
				current.remove();
			}
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 初始化
		 */
		private function init():void
		{
			this.dialogs = new HashMap();
		}
		
		/**
		 * 处理指定容器队列
		 * @param container
		 */
		private function progress(container:DisplayObjectContainer):void
		{
			var queue:Vector.<Dialog> = this.dialogs.getValue(container);
			
			if (queue == null)
			{
				return;
			}
			
			if (queue.length <= 0)
			{
				this.dialogs.remove(container);
				return;
			}
			
			var current:Dialog = queue[0];
			
			if (container.contains(current))
			{
				return;
			}
			
			current.addEventListener(Event.ADDED_TO_STAGE, onDialogAdded, false, 0, true);
			container.addChild(current);
			
			this.dispatchEvent(new DialogEvent(DialogEvent.ADDED, current));
		}
		
		/**
		 * 对话框添加到舞台时
		 * @param event
		 */
		private function onDialogAdded(event:Event):void
		{
			(event.target as Dialog).removeEventListener(Event.ADDED_TO_STAGE, onDialogAdded);
			(event.target as Dialog).addEventListener(Event.REMOVED_FROM_STAGE, onDialogRemoved, false, 0, true);
		}
		
		/**
		 * 对话框从舞台移除时
		 * @param event
		 */
		private function onDialogRemoved(event:Event):void
		{
			(event.target as Dialog).removeEventListener(Event.REMOVED_FROM_STAGE, onDialogRemoved);
			
			var container:DisplayObjectContainer = (event.target as Dialog).parent;
			
			var queue:Vector.<Dialog> = this.dialogs.getValue(container);
			delete queue.shift();
			
			this.dispatchEvent(new DialogEvent(DialogEvent.REMOVED, event.target as Dialog));
			
			this.progress(container);
		}
	
	}
}
